package com.vm.activity;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonArrayRequest;
import com.vm.commons.CurrentUser;
import com.vm.commons.VMConst;
import com.vm.model.UserOrder;
import com.vm.myview.ViewUtils;
import com.vm.utils.JsonUtils;
import com.vm.utils.MyVolley;
import com.wwtscapp.apk.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年3月18日 下午3:14:06]
 * @Version: [v1.0]
 * 
 */
public class OrderActivity extends Activity {

	protected static final String TAG = "OrderActivity";
	RequestQueue mQueue;
	List<UserOrder> list;
	ImageView imgbtn_search_back;
	int pageNow = 1;
	boolean canRequest = false;
	ScrollView scrollview_list;
	LinearLayout found_ll_1;
	ViewUtils viewUtils;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order);
		init();

	}

	public void onResume() {
		init();
		super.onResume();
	}

	/**
	 * 初始化
	 */
	private void init() {
		scrollview_list = (ScrollView) findViewById(R.id.scrollview_list);
		scrollview_list.setOnTouchListener(new TouchListenerImpl());
		imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
		imgbtn_search_back.setOnClickListener(btn_backListener);
		found_ll_1 = (LinearLayout) findViewById(R.id.found_ll_1);
		viewUtils = new ViewUtils(this);
		mQueue = MyVolley.getRequestQueue();
		list = new ArrayList<UserOrder>();
		String url = VMConst.REQUEST_URL + "android_order?userId="
				+ CurrentUser.USERID;
		JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(url,
				new Listener<JSONArray>() {
					@Override
					public void onResponse(JSONArray arg0) {
						// arg0.length();
						// pageNow = 0;
						found_ll_1.removeAllViews();
						for (int i = 0; i < arg0.length(); i++) {
							if (arg0.length() == 10) {
								canRequest = true;
							}
							try {
								JSONObject jsonObject = arg0.getJSONObject(i);
								UserOrder userOrder = JsonUtils.jsonToBean(
										jsonObject, UserOrder.class);
								if (userOrder != null) {
									found_ll_1.addView(viewUtils.dyAddView3(i,
											userOrder));
								}
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						// canRequest = true;
						//
						// Log.i(TAG, Integer.toString(listsofts.size()));
						// // arg0.getJSONObject(index);

					}

				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e(TAG, error.getMessage(), error);
					}
				});

		mQueue.add(jsonObjectRequest);

	}

	/**
	 * imgbtn_search_back 返回按钮监听
	 */
	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			onBackPressed();
		}
	};

	/**
	 * 手机自带返回按钮监听
	 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}

	private class TouchListenerImpl implements OnTouchListener {

		@SuppressLint("ClickableViewAccessibility")
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				break;
			case MotionEvent.ACTION_MOVE:
				int scrollY = v.getScrollY();
				int height = v.getHeight();
				int scrollViewMeasuredHeight = scrollview_list.getChildAt(0)
						.getMeasuredHeight();
				if (scrollY == 0) {
					System.out.println("滑动到了顶端 view.getScrollY()=" + scrollY);
				}
				if ((scrollY + height) == scrollViewMeasuredHeight) {
					if (canRequest) {
						canRequest = false;
						System.out.print("canRequest:" + canRequest);
						pageNow++;
						System.out.print("pageNow:" + pageNow);
						String url = VMConst.REQUEST_URL
								+ "android_order?userId=" + CurrentUser.USERID
								+ "&pageNow=" + pageNow;
						JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(
								url, new Listener<JSONArray>() {
									@Override
									public void onResponse(JSONArray arg0) {
										// arg0.length();
										// pageNow = 0;
										for (int i = 0; i < arg0.length(); i++) {
											if (arg0.length() == 10) {
												canRequest = true;
											}
											try {
												JSONObject jsonObject = arg0
														.getJSONObject(i);
												UserOrder userOrder = JsonUtils
														.jsonToBean(jsonObject,
																UserOrder.class);
												if (userOrder != null) {
													found_ll_1.addView(viewUtils
															.dyAddView3(i,
																	userOrder));
												}
											} catch (JSONException e) {
												// TODO Auto-generated catch
												// block
												e.printStackTrace();
											}
										}
										//
										// Log.i(TAG,
										// Integer.toString(listsofts.size()));
										// // arg0.getJSONObject(index);

									}

								}, new Response.ErrorListener() {
									@Override
									public void onErrorResponse(
											VolleyError error) {
										Log.e(TAG, error.getMessage(), error);
									}
								});

						mQueue.add(jsonObjectRequest);
					}
				}
				break;
			}
			return false;
		}

	}
}
